<template>
	<view class="content">
		<view class="title">评价星级</view>
		<uni-rate :size="20" :value="apply_star_num" @change="changeStar"></uni-rate>
		<view class="title">评价内容</view>
		<textarea class="cont" placeholder-style="color:#CCCCCC" placeholder="请输入评价内容" maxlength="200" @input="descInput"></textarea >
		<view class="num">{{remnant}}/200</view>
		<view class="button" @tap="commit">提交评价</view>
	</view>
</template>

<script>
	import uniRate from "@/components/uni-rate/uni-rate.vue"
	export default{
		data() {
			return {
				remnant: 0, //文本域内容的长度
				tid: '',
				apply_text: '', //评价内容
				apply_star_num: 1 ,//默认星级为一级
				type: 1,
				userInfo: uni.getStorageSync('userInfo'),
				unid: '',
				package_id: ''
			}
		},
		components:{
			uniRate
		},
		onLoad(e) {
			this.tid = e.tid;
			if(e.type) {
				this.type = e.type;
				this.unid = e.unid;
				this.package_id = e.package_id;
			}
		},
		methods:{
			/* 获取评分 */
			changeStar(e) {
				this.apply_star_num = e.value;
			},
			/* 获取文本域的长度及内容 */
			descInput(e){
				this.remnant = e.detail.cursor;
				this.apply_text = e.detail.value;
				if(this.remnant >= 200) {
					uni.showToast({
						title: '最多只能输入200个字',
						icon: 'none'
					})
				}
			},
			commit() {
				if(this.type == 1) {
					this.$zapi.apiPost('user/resources.lists/GetResApplyByApplyid',{tid:this.tid}).then(res=>{
						if(res.data.code == 200) {
							if(res.data.data.is_apply == 'TRUE'){
								uni.showToast({
									title: '您已经评价过了，不能再次评价',
									icon: 'none'
								})
							} else {
								var data = {
									tid: this.tid,
									apply_text: this.apply_text,
									apply_star_num: this.apply_star_num
								}
								this.$zapi.apiPost('user/resources.lists/ResEvaluateAdd',data).then(res=>{
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									})
									if(res.data.code == 200) {
										setTimeout(function(){
											uni.navigateBack({
												delta: 1
											})
										},1000)
									}
								})
							}
						}
					})
				} else if (this.type == 2) {
					var data = {
						unid: this.unid,
						package_id: this.package_id,
						usr_head: this.userInfo.avatar,
						usr_name: this.userInfo.nicheng,
						context: this.apply_text,
						level: this.apply_star_num
					}
					this.$papi.apiPost('moblie/comment/save', data).then(res => {
						if (res.data.code == 200) {
							var data = res.data.data;
							this.apply_star_num = data.level;
							this.apply_text = data.context;
							this.isTrue = true;
							uni.navigateBack({
								delta: 1
							})
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'none'
							});
						}
					});
				}
			}
		}
	}
</script>

<style>
	.content{
		padding: 40upx;
	}
	.title{
		color: #666666;
		font-size: 34upx;
		line-height: 1;
		margin-bottom: 30upx;
	}
	.uni-rate{
		margin-bottom: 50upx;
	}
	.cont{
		color: #333333;
		font-size: 34upx;
		width: calc(100% - 60upx);
		background-color: #FAFAFA;
		padding: 30upx;
	}
	.num{
		margin-top: -50rpx;
		position: absolute;
		right: 60rpx;
		color: #CCCCCC;
	}
	.button{
		color: #FFFFFF;
		font-size: 34upx;
		background: #FFB800;
		text-align: center;
		height: 80upx;
		line-height: 80upx;
		position: fixed;
		bottom: 30upx;
		width: 690upx;
		margin: 0 auto;
	}
</style>
